package org.dromara.work.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.work.domain.BeianAuditFile;
import org.dromara.work.domain.bo.BeianAuditFileSaveBo;
import org.dromara.work.domain.vo.BeianAuditFileVo;
import org.dromara.work.mapper.BeianAuditFileMapper;
import org.dromara.work.service.IBeianAuditFileService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数公基平台备案附件Service业务层处理
 *
 * @author Chen
 * @date 2024-01-19
 */
@RequiredArgsConstructor
@Service
public class BeianAuditFileServiceImpl implements IBeianAuditFileService {

    private final BeianAuditFileMapper baseMapper;

    /**
     * 查询数公基平台备案附件
     */
    @Override
    public BeianAuditFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询数公基平台备案附件列表
     */
    @Override
    public TableDataInfo<BeianAuditFileVo> queryPageList(BeianAuditFileSaveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BeianAuditFile> lqw = buildQueryWrapper(bo);
        Page<BeianAuditFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数公基平台备案附件列表
     */
    @Override
    public List<BeianAuditFileVo> queryList(BeianAuditFileSaveBo bo) {
        LambdaQueryWrapper<BeianAuditFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BeianAuditFile> buildQueryWrapper(BeianAuditFileSaveBo bo) {
        LambdaQueryWrapper<BeianAuditFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), BeianAuditFile::getCode, bo.getCode());
        return lqw;
    }

    /**
     * 新增数公基平台备案附件
     */
    @Override
    public Boolean insertByBo(BeianAuditFileSaveBo bo) {
        //BeianAuditFile add = BeanUtil.toBean(bo, BeianAuditFile.class);
        //validEntityBeforeSave(add);
        //boolean flag = baseMapper.insert(add) > 0;
        boolean flag = baseMapper.insertGeoEntity(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改数公基平台备案附件
     */
    @Override
    public Boolean updateByBo(BeianAuditFileSaveBo bo) {
        BeianAuditFile update = BeanUtil.toBean(bo, BeianAuditFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BeianAuditFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数公基平台备案附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
